{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "tags": [
     "remove-cell"
    ]
   },
   "outputs": [],
   "source": [
    "import sys\n",
    "import os\n",
    "if not any(path.endswith('textbook') for path in sys.path):\n",
    "    sys.path.append(os.path.abspath('../../..'))\n",
    "from textbook_utils import *          \n",
    "\n",
    "from sklearn.linear_model import LinearRegression\n",
    "from sklearn.metrics import mean_squared_error\n",
    "from sklearn.model_selection import train_test_split"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(ch:viz_other_tools)=\n",
    "# Other Tools for Visualization\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are many software packages and tools for creating data visualizations.\n",
    "In this book, we primarily use `plotly`.\n",
    "But it's worth knowing about a few other commonly used tools.\n",
    "In this section, we compare `plotly` to `matplotlib` and to grammar of graphics\n",
    "tools."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## matplotlib\n",
    "\n",
    "The library [`matplotlib`](https://matplotlib.org/) is one of the first data\n",
    "visualization tools created for Python. Because of this, it is widely used and\n",
    "has a large ecosystem of packages. Notably, the built-in plotting methods for\n",
    "`pandas` dataframes make plots using `matplotlib`. One popular package that\n",
    "builds on top of `matplotlib` is called [`seaborn`](https://seaborn.pydata.org/).\n",
    "Compared to `matplotlib` alone, `seaborn` provides a much simpler API to create\n",
    "statistical plots, like dot plots with confidence intervals. In fact,\n",
    "`seaborn`'s API was used as an inspiration for `plotly`'s API. If you look at\n",
    "`plotly` code and `seaborn` code side by side, you'll find that the methods to\n",
    "create basic plots use similar code.\n",
    "\n",
    "One advantage of using `matplotlib` is its popularity. It's relatively easy to\n",
    "find help creating or fine-tuning plots online because many existing projects\n",
    "use it. For this book, the main advantage of using `plotly` is that the plots\n",
    "we create are interactive. Plots in `matplotlib` are usually static images,\n",
    "which don't allow for panning, zooming, or hovering over marks. Still, we\n",
    "expect that `matplotlib` will continue to be used for data analyses. In fact,\n",
    "several of the plots in this book were made using `seaborn` and `matplotlib`\n",
    "because `plotly` doesn't yet support all the plots we want to make."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Grammar of Graphics \n",
    "\n",
    "The [grammar of graphics](https://dl.acm.org/doi/book/10.5555/1088896) is a theory developed by Lee Wilkinson for creating data visualizations.\n",
    "The basic idea is to use common building blocks for making plots.\n",
    "For instance, a bar plot and a dot plot are nearly identical, except that \n",
    "a bar plot draws rectangles and a dot plot draws points.\n",
    "This idea is captured in the grammar of graphics, which would say that\n",
    "a bar plot and a dot plot differ only in their \"geometry\" component.\n",
    "The grammar of graphics is an elegant system that we can use to describe \n",
    "nearly every kind of plot we wish to make.\n",
    "\n",
    "This system is implemented in the popular plotting libraries [`ggplot2`](https://ggplot2.tidyverse.org/) for the\n",
    "R programming language and [`Vega`](https://vega.github.io/vega/) for JavaScript.\n",
    "A Python package called [`Vega-Altair`](https://altair-viz.github.io/) provides a way to create `Vega` plots using\n",
    "Python, and we encourage interested readers to look over its documentation.\n",
    "\n",
    "Using a grammar of graphics tool like `Vega-Altair` enables flexibility in visualizations.\n",
    "And like `plotly`, `altair` also creates interactive visualizations.\n",
    "However, the Python API for these tools can be less straightforward than\n",
    "`plotly`'s API.\n",
    "In this book, we don't typically need plots outside of what `plotly` is capable\n",
    "of creating, so we have opted for `plotly`'s simpler API. "
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are many more plotting tools for Python that we've left out for brevity.\n",
    "But for the purposes of this book, relying on `plotly` provides a useful\n",
    "balance of interactivity and flexibility."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
